package com.palandrodev.calsmart;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.palandrodev.calsmart.base.Tip;
import com.palandrodev.calsmart.util.Constants;
import com.palandrodev.calsmart.util.NumberTextWatcher;

public class TipCalculatorActivity extends Activity {
	private TextView tipCalculatorViewHeading;
	private EditText amountInput;
	private EditText percentageInput;
	private EditText numberPersonInput;
	private TextView tipAmountOutput;
	private TextView totalAmountOutput;
	private TextView splitAmountOutput;
	private Button tipCalculateButton;

	public TextView getTipCalculatorViewHeading() {
		return tipCalculatorViewHeading;
	}

	public void setTipCalculatorViewHeading(TextView tipCalculatorViewHeading) {
		this.tipCalculatorViewHeading = tipCalculatorViewHeading;
	}

	public EditText getAmountInput() {
		return amountInput;
	}

	public void setAmountInput(EditText amountInput) {
		this.amountInput = amountInput;
	}

	public EditText getPercentageInput() {
		return percentageInput;
	}

	public void setPercentageInput(EditText percentageInput) {
		this.percentageInput = percentageInput;
	}

	public EditText getNumberPersonInput() {
		return numberPersonInput;
	}

	public void setNumberPersonInput(EditText numberPersonInput) {
		this.numberPersonInput = numberPersonInput;
	}

	public TextView getTipAmountOutput() {
		return tipAmountOutput;
	}

	public void setTipAmountOutput(TextView tipAmountOutput) {
		this.tipAmountOutput = tipAmountOutput;
	}

	public TextView getTotalAmountOutput() {
		return totalAmountOutput;
	}

	public void setTotalAmountOutput(TextView totalAmountOutput) {
		this.totalAmountOutput = totalAmountOutput;
	}

	public TextView getSplitAmountOutput() {
		return splitAmountOutput;
	}

	public void setSplitAmountOutput(TextView splitAmountOutput) {
		this.splitAmountOutput = splitAmountOutput;
	}

	public Button getTipCalculateButton() {
		return tipCalculateButton;
	}

	public void setTipCalculateButton(Button tipCalculateButton) {
		this.tipCalculateButton = tipCalculateButton;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tip_calculator_view);
		initialize();
	}

	private void initialize() {
		setTipCalculatorViewHeading((TextView) findViewById(R.id.TipcalculatorViewHeading));
		setAmountInput((EditText) findViewById(R.id.amountInput));
		setPercentageInput((EditText) findViewById(R.id.percentageInput));
		setNumberPersonInput((EditText) findViewById(R.id.numberPersonInput));
		setTipAmountOutput((TextView) findViewById(R.id.tipAmountOutput));
		setTotalAmountOutput((TextView) findViewById(R.id.totalAmountOutput));
		setSplitAmountOutput((TextView) findViewById(R.id.splitAmountOutput));
		setTipCalculateButton((Button) findViewById(R.id.tipcalculateButton));
		tipCalculatorViewHeading.setText("Tip Calculator");

		amountInput.setText("0.0");
		percentageInput.setText("0.0");
		numberPersonInput.setText("0");
		amountInput.addTextChangedListener(new NumberTextWatcher(amountInput));
		percentageInput.addTextChangedListener(new NumberTextWatcher(
				percentageInput));
		numberPersonInput.addTextChangedListener(new NumberTextWatcher(
				numberPersonInput));

		tipCalculateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				float amount = 0.0f, percentage = 0.0f;
				int numberPerson = 0;
				NumberFormat nf = new DecimalFormat("#,###.00");
				try {
					amount = Float.valueOf(amountInput.getText().toString()
							.replaceAll(",", ""));
					percentage = Float.valueOf(percentageInput.getText()
							.toString().replaceAll(",", ""));
					numberPerson = Integer.valueOf(numberPersonInput.getText()
							.toString().replaceAll(",", ""));

					Tip tip = new Tip(amount, percentage, numberPerson);
					tip.calculate();

					String formattedTipAmount = nf.format(tip.getTip_amount());
					String formattedTotalAmount = nf.format(tip
							.getTotal_amount());
					String formattedIndividualShare = nf.format(tip
							.getIndividual_share());
					tipAmountOutput.setText("$ " + formattedTipAmount);
					totalAmountOutput.setText("$ " + formattedTotalAmount);
					splitAmountOutput.setText("$ " + formattedIndividualShare);
				} catch (Exception ex) {
					Log.d(Constants.APP_NAME, ex.getMessage());
				}

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.menu_settings:
			Intent i = new Intent(this, ShowSettingsActivity.class);
			startActivityForResult(i, Constants.RESULT_SETTINGS);
			break;

		}

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Constants.RESULT_SETTINGS:

			break;

		}

	}

}
